package com.yxh.lib_base.ext.recycler

import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.OrientationHelper
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ResourceUtils
import com.yxh.lib_base.R
import com.yxh.lib_base.ext.recycler.div.ItemDecorationDivider
import com.yxh.lib_base.ext.recycler.div.ItemDecorationSpaces

/***
 * 点击事件的View扩展
 * @param block: (T) -> Unit 函数
 * @return Unit
 */

fun RecyclerView.initRcy(
    layoutManager: RecyclerView.LayoutManager, mAdapter: RecyclerView.Adapter<*>,
): RecyclerView {
    overScrollMode = RecyclerView.OVER_SCROLL_NEVER
    this.layoutManager = layoutManager
    this.adapter = mAdapter
    return this
}

fun RecyclerView.initRcyLinear(
    mAdapter: RecyclerView.Adapter<*>,
    isV: Boolean? = true,
): RecyclerView {
    overScrollMode = RecyclerView.OVER_SCROLL_NEVER
    val orientation = if (isV == true) RecyclerView.VERTICAL else RecyclerView.HORIZONTAL
    this.layoutManager = LinearLayoutManager(context, orientation, false)
    this.adapter = mAdapter
    return this
}

fun RecyclerView.initRcyGrid(mAdapter: RecyclerView.Adapter<*>, spanCount: Int): RecyclerView {
    overScrollMode = RecyclerView.OVER_SCROLL_NEVER
    this.layoutManager = GridLayoutManager(context, spanCount, RecyclerView.VERTICAL, false)
    this.adapter = mAdapter
    return this
}


//div
infix fun RecyclerView.addItemDecorationDiv(decoration: ItemDecorationDivider.() -> Unit): RecyclerView {
    val orientation = if (layoutManager != null && layoutManager is LinearLayoutManager) {
        (layoutManager as LinearLayoutManager).orientation
    } else {
        OrientationHelper.VERTICAL
    }
    var itemDividerDecoration = ItemDecorationDivider(
        context, orientation
    ).apply {
        setDrawable(ResourceUtils.getDrawable(R.drawable.shape_divider_e2))
    }
    decoration.invoke(itemDividerDecoration)
    this.addItemDecoration(itemDividerDecoration)
    return this
}


fun RecyclerView.addItemDecorationSpaces(
    space_h: Int, space_v: Int, itemDecoration: (ItemDecorationSpaces.() -> Unit)? = null,
): RecyclerView {
    var itemSpacesDecoration = ItemDecorationSpaces(space_h, space_v).apply {
        if (layoutManager != null && layoutManager is LinearLayoutManager) {
            asOrientation((layoutManager as LinearLayoutManager).orientation)
        }
    }
    itemDecoration?.invoke(itemSpacesDecoration)
    this.addItemDecoration(itemSpacesDecoration)
    return this
}









